package org.jeecg.modules.yks.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.yks.dev.entity.YksEquipmentDevops;
import org.jeecg.modules.yks.dev.service.IYksEquipmentDevopsService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 发送运维消息任务
 * @author: jeecg-boot
 */

@Slf4j
public class SendDevopsJob implements Job {
    @Autowired
    private IYksEquipmentDevopsService yksEquipmentDevopsService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        log.info(String.format(" Jeecg-Boot 发送消息任务 SendMsgJob !  时间:" + DateUtils.getTimestamp()));
        // 1.读取运维数据，只查询当天未运维的数据
        QueryWrapper<YksEquipmentDevops> queryWrapper = new QueryWrapper<YksEquipmentDevops>();
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        queryWrapper.eq("maintenance_next_time", DateUtils.formatDate()).lt("maintenance_status",1);
        List<YksEquipmentDevops> yksEquipmentDevopss = yksEquipmentDevopsService.list(queryWrapper);
        System.out.println(yksEquipmentDevopss);
        // 2.根据不同的类型走不通的发送实现类
        for (YksEquipmentDevops yksEquipmentDevops : yksEquipmentDevopss) {
            //update-begin-author:taoyan date:2022-7-8 for: 模板消息发送测试调用方法修改
//            Integer sendNum = sysMessage.getEsSendNum();
            try {
                LoginUser sysUser = sysBaseAPI.getUserById(yksEquipmentDevops.getMaintenancePerson());
                MessageDTO md = new MessageDTO();
                md.setTitle(yksEquipmentDevops.getName());
                md.setContent(yksEquipmentDevops.getMaintenanceContext());
                md.setToUser(sysUser.getUsername());
                md.setFromUser("admin");
                md.setCategory("1");//保养任务
                //md.setToAll(true);
                sysBaseAPI.sendSysAnnouncement(md);
                //发送消息成功
//                sysMessage.setEsSendStatus(SendMsgStatusEnum.SUCCESS.getCode());
                //update-end-author:taoyan date:2022-7-8 for: 模板消息发送测试调用方法修改
            } catch (Exception e) {
                e.printStackTrace();
                // 发送消息出现异常
//                sysMessage.setEsSendStatus(SendMsgStatusEnum.FAIL.getCode());
            }
//            sysMessage.setEsSendNum(++sendNum);
            // 发送结果回写到数据库
//            sysMessageService.updateById(sysMessage);
        }
    }
}
